package org.xy.parser;

import java.util.Arrays;
import java.util.Map;

import org.xy.bean.Bean;
import org.xy.bean.BeanWapper;

public class BeanDefinitionParser extends RootParser {

	public BeanDefinitionParser(Map<String, Bean> beans) {
		super(beans);
	}

	@Override
	protected BeanWapper getBeanWapper(String beanName) {
		if (this.exist(beanName)) {
			this.logger.info("Exist BeanWapper[" + beanName
					+ "] in map, return it directly");
			return beaWappers.get(beanName);
		}

		Bean current = this.beans.get(beanName);
		BeanWapper beanWapper = new BeanWapper(current);
		if (current.hasChild()) {
			this.logger.info("BeanWapper[" + beanName + "] has child"
					+ Arrays.toString(current.getChildNames().toArray()));
			for (String childName : current.getChildNames()) {
				this.logger.info("get child bean[" + childName + "]");
				beanWapper.addChild(this.getBeanWapper(childName));
			}

		}
		beaWappers.put(beanName, beanWapper);
		this.logger.info("put BeanWapper[" + beanName + "] into map");
		return beanWapper;
	}

	// @Override
	// protected Map<String, BeanWapper> getBeanWappers() {
	// if (this.beans == null || this.beans.isEmpty()) {
	// return null;
	// }
	//
	// for (String beanName : this.beans.keySet()) {
	// this.logger.info("get BeanWapper[" + beanName + "]");
	//
	// if (!this.exist(beanName)) {
	// BeanWapper beanWapper = this.getBeanWapper(beanName);
	//
	// } else {
	// this.logger.info("Exist BeanWapper[" + beanName
	// + "] in map, don't instant it anymore");
	// }
	// this.logger.info("");
	// }
	//
	// return beaWappers;
	// }
}
